﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using System.IO;
using System.Reflection;

namespace PaymentHelper.Core.ViewEmbeddedHandler
{
    public class EmbeddedResourceVirtualFile : VirtualFile
    {
        private readonly EmbeddedViewData _embeddedViewData;

        public EmbeddedResourceVirtualFile(EmbeddedViewData embeddedViewData, string virtualPath):base(virtualPath)
        {
            if(embeddedViewData == null)
                throw new PaymentHelper.Core.ExceptionHandler.PaymentHelperException(ExceptionHandler.ExceptionLevel.Warning, "EmbeddedResourceVirtualFile", "Argument embeddedViewsData in contructor is null");
            
            this._embeddedViewData = embeddedViewData;
        }

        public override Stream Open()
        {
            Assembly assembly = AssemblyLoader.LoadResourceAssembly(_embeddedViewData.AssemblyFullName);
            
            return (assembly != null) ? assembly.GetManifestResourceStream(_embeddedViewData.ViewName) : null;
        }
    }
}
